﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.Util;
using HsTec.Mobile.Green.BLL;
using Baimasoft.Manage.SqlServerDAL;
using System.Text;
using System.Web.UI;
using System.Web;
using System.IO;


public partial class ReportAnalysis_MonthlyReportManage : System.Web.UI.Page
{
    ReportAnalysisBLL bll = new ReportAnalysisBLL();
    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        lstArea.Attributes.Add("onclick", "FindSelectedItems(this," + txtArea.ClientID + ");");
        chkArea.Attributes.Add("onclick", "chkAll(this)");
        if (!this.IsPostBack)
        {
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {

            }
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = " ASC";
            }
            if (ViewState["sortExpression"] == null)
            {
                ViewState["sortExpression"] = String.Empty;
            }
            //this.BindGridViewData();
            this.sltAreaBind();
            sltStartYear.Value = DateTime.Now.Year.ToString();
            sltStartMonth.Value = string.Format("{0:D2}", DateTime.Now.Month);
            sltEndYear.Value = DateTime.Now.Year.ToString();
            sltEndMonth.Value = string.Format("{0:D2}", DateTime.Now.Month);
            this.HiddenID.Value = "";
        }
    }
    private void BindGridViewData()
    {
        DataView mydv = bll.GetList(GetSearchString());
        if (!String.IsNullOrEmpty(ViewState["sortExpression"].ToString()))
            mydv.Sort = ViewState["sortExpression"].ToString() + ViewState["sortDirection"].ToString();
        GridView1.DataSource = mydv;
        GridView1.DataBind();
        this.HiddenID.Value = "";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {

        }
    }
    private string GetSearchString()
    {
        string searchStr = "(1=1)";

        string sArea = String.Empty;
        for (int i = 0; i < lstArea.Items.Count; i++)
        {
            if (lstArea.Items[i].Selected == true)
            {
                sArea += sArea == String.Empty ? lstArea.Items[i].Value : "," + lstArea.Items[i].Value;
            }
        }
        if (sArea != "")
        {
            searchStr += " and Area_ID in (" + sArea + ") ";
        }
        searchStr += " and Years  >='" + sltStartYear.Value + sltStartMonth.Value + "'  and Years  <='" + sltEndYear.Value + sltEndMonth.Value + "' ";
        return searchStr;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(HiddenID.Value.Trim()))
        {
            bll.Delete(HiddenID.Value.TrimEnd(','));
            BindGridViewData();
            this.HiddenID.Value = "";
        }
    }
    protected void linkBtnGo_Click(object sender, EventArgs e)
    {

    }
    private void sltAreaBind()
    {
        lstArea.DataSource = bll.GetAreaList();
        lstArea.DataTextField = "Name";
        lstArea.DataValueField = "ID";
        lstArea.DataBind();
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sortDirection"].ToString() == " ASC")
        {
            ViewState["sortDirection"] = " DESC";
        }
        else
        {
            ViewState["sortDirection"] = " ASC";
        }
        ViewState["sortExpression"] = e.SortExpression;
        this.BindGridViewData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (e.NewPageIndex > 0)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGridViewData();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridViewData();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            HttpContext curContext = System.Web.HttpContext.Current;
            StringWriter strWriter = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            curContext.Response.Charset = "GB2312";
            DataView mydv = bll.GetListExport(GetSearchString());
            if (!String.IsNullOrEmpty(ViewState["sortExpression"].ToString()))
                mydv.Sort = ViewState["sortExpression"].ToString() + ViewState["sortDirection"].ToString();
            GridView GV = new GridView();
            GV.DataSource = mydv;
            GV.DataBind();
            GV.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
        catch
        {

        }
    }
}
